#====# Appendix L: Robustness to alternative research designs (diff-in-diff and GSC) #====#

# Load libraries and set defaults ----
library(fixest)
library(gsynth)
library(bizdays)
library(modelsummary)
library(tinytable)
library(lemon)
library(ggpubr)
library(tictoc)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/plot_theme.R")

# prepare business calendar:
business_calendar <- create.calendar('biz_calendar', weekdays = c('saturday','sunday'))

# Import data ----
stocks <- read_rds("data_out/stocks_analysis.rds") # main analysis dataset

# Table L.1: Difference-in-differences models of price at closing ----
mod_did <- feols(prccd ~ post_event * FCPA_sample,
                 data = stocks %>%
                   mutate(post_event = as.numeric(date >= event)) %>%
                   filter(date >= offset(event, -5, business_calendar) &
                            date <= offset(event, +5, business_calendar)),
                 cluster = ~ticker_symbol)

mod_did2 <- feols(prccd ~ post_event * FCPA_sample,
                  data = stocks %>%
                    filter(matched == 1) %>%
                    mutate(post_event = as.numeric(date >= event)) %>%
                    filter(date >= offset(event, -5, business_calendar) &
                             date <= offset(event, +5, business_calendar)),
                  cluster = ~ticker_symbol)

rows <- tribble(~term, ~mod_did, ~mod_did2,
                "Std.Errors", "by: firm", "by: firm")
attr(rows, "position") <- c(9)

modelsummary(list(mod_did, mod_did2),
             stars = c("*" = 0.05),
             escape = FALSE,
             coef_map = c("post_event" = "Post-Executive Order",
                          "FCPA_sample" = "Past FCPA target",
                          "post_event:FCPA_sample" = "Post-Executive Order $\\times$ Past FCPA target",
                          "(Intercept)" = "(Intercept)"),
             gof_omit = "R2 Within|AIC|BIC|RMSE|FE|Std.Errors",
             notes = paste0("Linear regression models to estimate Difference-in-differences of stock price at closing ",
                            "for past FCPA targets and matched placebo firms after the Executive Order. ",
                            "``Post-Executive Order'' is a binary taking value of 1 on the day of February 10th 2025 and after. ",
                            "``Past FCPA target'' is a binary taking value of 1 for firms that have been targets of an FCPA ",
                            "investigation or enforcement action in the past. The table considers data relative to the entire ",
                            "trading week before the Executive Order and the trading week of the Executive Order itself. ",
                            "Standard errors clustered by firm in parentheses. ",
                            collapse = ""),
             title = "Difference-in-differences models of price at closing \\label{tab:rob_did}",
             add_rows = rows) %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htb") %>%
  group_tt(j = list("All past targets" = 2,
                    "Only matched past targets" = 3)) %>%
  save_tt("tables/table_L1.html", overwrite = TRUE)

# Figure L.1: DiD event analysis of firms’ price at closing following the FCPA Executive Order ----
feols(prccd ~ i(date, FCPA_sample, ref = as.Date("2025-02-07")) | ticker_symbol + date,
      data = stocks %>%
        filter(date >= offset(event, -10, business_calendar)),
      cluster = ~ticker_symbol) %>%
  broom::tidy(conf.int = TRUE) %>%
  mutate(term = term %>%
           str_remove("^.+\\:\\:") %>%
           str_remove("\\:.+$") %>%
           as.Date()) %>%
  bind_rows(data.frame(term = as.Date("2025-02-07"), estimate = 0)) %>%
  mutate(type = "Compare all past FCPA defendants with matched placebo firms") %>%
  rbind(feols(prccd ~ i(date, FCPA_sample, ref = as.Date("2025-02-07")) | ticker_symbol + date,
              data = stocks %>%
                filter(matched == 1) %>%
                filter(date >= offset(event, -10, business_calendar)),
              cluster = ~ticker_symbol) %>%
          broom::tidy(conf.int = TRUE) %>%
          mutate(term = term %>%
                   str_remove("^.+\\:\\:") %>%
                   str_remove("\\:.+$") %>%
                   as.Date()) %>%
          bind_rows(data.frame(term = as.Date("2025-02-07"), estimate = 0)) %>%
          mutate(type = "Compare matched past FCPA defendants with matched placebo firms")) %>%
  mutate(type = fct_relevel(type, "Compare all past FCPA defendants with matched placebo firms")) %>%
  ggplot(aes(x = term, y = estimate)) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) +
  geom_line(linetype = "dashed") +
  geom_label(data = data.frame(x = as.Date("2025-02-04"), 
                               y = 6.5),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-02-05"), 
                               xend = as.Date("2025-02-10")-0.6,
                               y = 4.5, 
                               yend = 2),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = 0.2) +
  xlab("") + ylab("ATT on price per share at closing") +
  scale_y_continuous(labels = ~paste0(.x, "$")) +
  facet_rep_wrap("type", nrow = 2, repeat.tick.labels = "all")
ggsave("plots/figure_L1.pdf", width = 8, height = 4)

# Figure L.2: Generalized synthetic control results (full sample) ----
tic()
out <- gsynth(formula = prccd ~ treat,
              data = stocks %>%
                filter(date >= as.Date("2025-01-01")) %>%
                mutate(treat = as.numeric(FCPA_sample == 1 & date >= as.Date("2025-02-10"))) %>%
                as.data.frame(),
              Y = "prccd", D = "treat", na.rm = TRUE, index = c("ticker_symbol", "date"),
              se = TRUE, cores = 6, seed = 160493)
toc() # 7 sec when imposing 6 cores for parallel computation

p1 <- out$est.att %>%
  as.data.frame() %>%
  mutate(date = out$time) %>%
  ggplot(aes(x = date, y = ATT)) +
  geom_point(shape = 4) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_ribbon(aes(ymin = CI.lower, ymax = CI.upper), alpha = .3) +
  geom_line() +
  geom_label(data = data.frame(x = as.Date("2025-01-20"), 
                               y = 3),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-01-25"), 
                               xend = as.Date("2025-02-10")-1,
                               y = 3.1, 
                               yend = 2.8),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  xlab("") + ylab("ATT on price per share at closing") +
  scale_y_continuous(labels = ~paste0(.x, "$"))

p2 <- out$Y.bar %>%
  as.data.frame() %>%
  mutate(date = out$time) %>%
  select(-Y.co.bar) %>%
  pivot_longer(-date) %>%
  ggplot(aes(x = date, y = value)) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  geom_line(aes(linetype = name, col = name)) +
  geom_point(aes(shape = name), fill = "white") +
  geom_label(data = data.frame(x = as.Date("2025-01-20"), 
                               y = 100),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-01-25"), 
                               xend = as.Date("2025-02-10")-1,
                               y = 100.1, 
                               yend = 99.8),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  scale_color_manual("", breaks = c("Y.tr.bar", "Y.ct.bar"),
                     labels = c("Treated (average)", "Synthetic counterfactual (average)"),
                     values = c("black", "grey50")) +
  scale_linetype_manual("", breaks = c("Y.tr.bar", "Y.ct.bar"),
                        labels = c("Treated (average)", "Synthetic counterfactual (average)"),
                        values = c("solid", "dashed")) +
  scale_shape_manual("", breaks = c("Y.tr.bar", "Y.ct.bar"),
                     labels = c("Treated (average)", "Synthetic counterfactual (average)"),
                     values = c(19, 21)) +
  xlab("") + ylab("Price per share at closing") +
  scale_y_continuous(labels = ~paste0(.x, "$"))

ggarrange(p1, p2, nrow = 2)
ggsave("plots/figure_L2.pdf", height = 8.5, width = 6)

# calculate effects:
effects <- (out$Y.tr - out$Y.ct) %>%
  as.data.frame() %>%
  as_tibble() %>%
  mutate(date = out$time) %>%
  relocate(date) %>%
  pivot_longer(-"date", 
               names_to = "ticker_symbol",
               values_to = "effect") %>%
  left_join(stocks %>%
              select(ticker_symbol, date, cshoq)) %>%
  mutate(diff_cap = cshoq * effect)

mean(effects$diff_cap[effects$date == as.Date("2025-02-10")], na.rm = TRUE)
# average gain in mkt cap on the day of the order: $785,305,548

sum(effects$diff_cap[effects$date == as.Date("2025-02-10")], na.rm = TRUE)
# total portfolio gain in mkt cap on the day of the order: $189,258,637,177

mean(effects$diff_cap[effects$date == as.Date("2025-03-25")], na.rm = TRUE)
# average gain in mkt cap 1 month after the order: $6,528,768,657 (!)

sum(effects$diff_cap[effects$date == as.Date("2025-03-25")], na.rm = TRUE)
# total portfolio gain in mkt cap 1 month after of the order: $1,573,433,000,000 (!!)

# Figure L.3: Generalized synthetic control results (only matched past FCPA targets) ----
tic()
out <- gsynth(formula = prccd ~ treat,
              data = stocks %>%
                filter(matched == 1) %>%
                filter(date >= as.Date("2025-01-01")) %>%
                mutate(treat = as.numeric(FCPA_sample == 1 & date >= as.Date("2025-02-10"))) %>%
                as.data.frame(),
              Y = "prccd", D = "treat", na.rm = TRUE, index = c("ticker_symbol", "date"),
              se = TRUE, cores = 6, seed = 160493)
toc() # 7 sec when imposing 6 cores for parallel computation

p1 <- out$est.att %>%
  as.data.frame() %>%
  mutate(date = out$time) %>%
  ggplot(aes(x = date, y = ATT)) +
  geom_point(shape = 4) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_ribbon(aes(ymin = CI.lower, ymax = CI.upper), alpha = .3) +
  geom_line() +
  geom_label(data = data.frame(x = as.Date("2025-01-20"), 
                               y = 3),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-01-25"), 
                               xend = as.Date("2025-02-10")-1,
                               y = 3.1, 
                               yend = 2.8),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  xlab("") + ylab("ATT on price per share at closing") +
  scale_y_continuous(labels = ~paste0(.x, "$"))

p2 <- out$Y.bar %>%
  as.data.frame() %>%
  mutate(date = out$time) %>%
  select(-Y.co.bar) %>%
  pivot_longer(-date) %>%
  ggplot(aes(x = date, y = value)) +
  geom_vline(xintercept = as.Date("2025-02-10")-0.5, linewidth = 0.1) +
  geom_line(aes(linetype = name, col = name)) +
  geom_point(aes(shape = name), fill = "white") +
  geom_label(data = data.frame(x = as.Date("2025-01-20"), 
                               y = 106),
             aes(x = x, y = y),
             label = "FCPA\nExecutive Order", label.size = NA,
             color = "black", fill = NA) +
  geom_curve(data = data.frame(x = as.Date("2025-01-25"), 
                               xend = as.Date("2025-02-10")-1,
                               y = 106.1, 
                               yend = 105.8),
             aes(x = x, y = y, yend = yend, xend = xend),
             arrow = arrow(length = unit(0.2, "cm")),
             linewidth = .5, col = I("black"),
             curvature = -0.2) +
  scale_color_manual("", breaks = c("Y.tr.bar", "Y.ct.bar"),
                     labels = c("Treated (average)", "Synthetic counterfactual (average)"),
                     values = c("black", "grey50")) +
  scale_linetype_manual("", breaks = c("Y.tr.bar", "Y.ct.bar"),
                        labels = c("Treated (average)", "Synthetic counterfactual (average)"),
                        values = c("solid", "dashed")) +
  scale_shape_manual("", breaks = c("Y.tr.bar", "Y.ct.bar"),
                     labels = c("Treated (average)", "Synthetic counterfactual (average)"),
                     values = c(19, 21)) +
  xlab("") + ylab("Price per share at closing") +
  scale_y_continuous(labels = ~paste0(.x, "$"))

ggarrange(p1, p2, nrow = 2)
ggsave("plots/figure_L3.pdf", height = 8.5, width = 6)

#====# The End #====#